<!DOCTYPE html>
<html>
<head>
    <title>Compton Scattering Diagram</title>
</head>
<body>
    <canvas id="compton-scattering-canvas" width="800" height="550"></canvas>
    <script>
        const canvas = document.getElementById('compton-scattering-canvas');
        const ctx = canvas.getContext('2d');

        // Styles
        ctx.fillStyle = 'black';
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 1.5;
        ctx.font = '22px sans-serif';
        ctx.textAlign = 'left';

        // --- 1. Incident Photon ---

        // Coordinates and parameters
        const startX = 50;
        const startY = 200;
        const collisionX = 380;
        const collisionY = 200;
        const waveStartX = 150;
        const waveLengthX = collisionX - waveStartX;
        const amplitude = 20;
        const visualWavelengthInc = 80;

        // Dashed line for path
        ctx.save();
        ctx.setLineDash([6, 6]);
        ctx.beginPath();
        ctx.moveTo(startX, startY);
        ctx.lineTo(collisionX, startY);
        ctx.stroke();
        ctx.restore();

        // Arrow on the path
        const arrowX = 100;
        const arrowLength = 30;
        ctx.beginPath();
        ctx.moveTo(arrowX, startY);
        ctx.lineTo(arrowX + arrowLength, startY);
        ctx.stroke();
        // Arrowhead
        ctx.beginPath();
        ctx.moveTo(arrowX + arrowLength, startY);
        ctx.lineTo(arrowX + arrowLength - 10, startY - 6);
        ctx.moveTo(arrowX + arrowLength, startY);
        ctx.lineTo(arrowX + arrowLength - 10, startY + 6);
        ctx.stroke();

        // Sine wave
        ctx.beginPath();
        ctx.moveTo(waveStartX, startY);
        for (let x_rel = 0; x_rel <= waveLengthX; x_rel++) {
            const y_rel = amplitude * Math.sin((x_rel / visualWavelengthInc) * 2 * Math.PI);
            ctx.lineTo(waveStartX + x_rel, startY + y_rel);
        }
        ctx.stroke();

        // Labels for incident photon
        ctx.fillText('incident photon', 140, 160);
        ctx.fillText('wavelength 540 nm', 140, 245);


        // --- 2. Electron ---

        const electronRadius = 10;
        ctx.beginPath();
        ctx.arc(collisionX, collisionY, electronRadius, 0, 2 * Math.PI);
        ctx.fill();

        // Label for electron
        const electronLabelX = 420;
        const electronLabelY = 120;
        ctx.fillText('electron', electronLabelX, electronLabelY);
        // Leader line
        ctx.beginPath();
        ctx.moveTo(electronLabelX + 40, electronLabelY + 20);
        ctx.lineTo(collisionX + electronRadius * 0.707, collisionY - electronRadius * 0.707);
        ctx.stroke();


        // --- 3. Deflected Photon ---

        // Parameters
        const angle = 40 * Math.PI / 180; // Deflection angle in radians
        const pathLength = 380;
        const visualWavelengthDefl = 100; // Visually longer than incident
        
        ctx.save();
        ctx.translate(collisionX, collisionY);
        ctx.rotate(angle);

        // Dashed line for path
        ctx.setLineDash([6, 6]);
        ctx.beginPath();
        ctx.moveTo(0, 0);
        ctx.lineTo(pathLength, 0);
        ctx.stroke();
        ctx.setLineDash([]);
        
        // Arrow at the end of the path
        const deflArrowStart = pathLength - 40;
        const deflArrowEnd = pathLength - 10;
        ctx.beginPath();
        ctx.moveTo(deflArrowStart, 0);
        ctx.lineTo(deflArrowEnd, 0);
        ctx.stroke();
        // Arrowhead
        ctx.beginPath();
        ctx.moveTo(deflArrowEnd, 0);
        ctx.lineTo(deflArrowEnd - 10, -6);
        ctx.moveTo(deflArrowEnd, 0);
        ctx.lineTo(deflArrowEnd - 10, 6);
        ctx.stroke();
        
        // Sine wave
        ctx.beginPath();
        ctx.moveTo(0, 0);
        for (let x_rel = 0; x_rel <= pathLength - 10; x_rel++) {
            const y_rel = amplitude * Math.sin((x_rel / visualWavelengthDefl) * 2 * Math.PI);
            ctx.lineTo(x_rel, y_rel);
        }
        ctx.stroke();
        ctx.restore();

        // Labels for deflected photon
        ctx.fillText('deflected photon', 560, 470);
        ctx.fillText('wavelength 544 nm', 550, 505);

        // --- 4. Figure Label ---
        ctx.textAlign = 'center';
        ctx.fillText('Fig. 11.1', canvas.width / 2, 540);

    </script>
</body>
</html>